﻿$(document).ready(function() {
    $('#sltAllRecommandCat')[0].checked = '';
    $('#RecommandCategoryList').bind('dblclick', function(e) {
        var ele = e.srcElement || e.target;
        var target = $(ele), data;
        if (target.is('ul'))
            return;
        else if (target.is("li > img")) {
            Jump2Node(target.parent().attr('catid'));
        }
    });
    $('#RecommandCategoryList').bind('click', function(e) {
        var ele = e.srcElement || e.target;
        var target = $(ele);
        if (target.is(':checkbox')) {
            if (!ele.checked) {
                $('#sltAllRecommandCat')[0].checked = '';
            } else {
                CheckSelectAllRecommandCatState();
            }
        } else if (target.is('img')) {
            switch (target.attr('tag')) {
                case 'folder_view':
                    var data = target.parent().parent().data("cat");
                    if (data) {
                        ViewCategory(data);
                    }
                    break;
                case 'folder_unrecommand':
                    var data = target.parent().parent().data("cat");
                    if (data && data.CategoryId) {
                        if (!confirm("您确认要取消 " + data.CategoryName + " 的推荐状态吗？")) return;
                        $('#content').masklayer('show');
                        RecommandCategory.UnRecommandCategory([data.CategoryId], function() {
                            RefreshRecommandCategory();
                            CheckSelectAllRecommandCatState();
                            $('#content').masklayer('hide');
                        }, function(err) {
                            alert('操作失败，错误信息：' + err + '。');
                            $('#content').masklayer('hide');
                        });
                    }
                    break;
                default:
                    return;
            }
            e.stopPropagation();
        }
    });
    RecommandCategory.onAdd = function(cId) {
        RefreshRecommandCategory();
        CheckSelectAllRecommandCatState();
    }
});

function AddRootCategory() {
    var name = $('#roocat_tbName').val();
    var order = $('#roocat_tbOrder').val();
    if (!name || $.trim(name) == '') {
        alert('请输入目录名。');
        return;
    }
    if (isNaN(order)) {
        alert('序号必须为数字或为空。');
        return;
    }
    $.ajax({
        url: "ajax.aspx?action=addrootcategory",
        async: false,
        dataType: "json",
        data: { "CategoryName": name, "Order": order },
        type: 'POST',
        beforeSend: function() {
            $('#content').masklayer('show', { Text: '正在保存，请稍候...' });
        },
        success: function(d) {
            d = eval(d);
            if (!d.Error) {
                ResetRootCategory();
                BindTreeNode($('#navtree'), null, false);
            } else {
                alert('保存失败！错误信息：' + d.Error);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert('发送请求失败！错误信息：' + errorThrown);
        },
        complete: function() {
            $('#content').masklayer('hide');
        }
    });
}

function ResetRootCategory() {
    $('#roocat_tbName').val('').focus();
    $('#roocat_tbOrder').val('');
}

function RefreshRecommandCategory() {
    var target = $('#RecommandCategoryList')
    target.empty();
    $.ajax({
        async: true,
        dataType: "json",
        url: "ajax.aspx?action=getrecommandcategory",
        beforeSend: function() { $('#tab-recommandcategory').masklayer('show', { Text: 'Loading Items...' }); },
        success: function(data, textStatus) {
            if (data) {
                if (data.Error) {
                    alert(data.Error);
                } else {
                    var i = 0, len = data.Result.length, d;
                    while (i < len) {
                        d = data.Result[i];
                        var li = $("<li><div><input type='checkbox' /><img title='查看信息' tag='folder_view' src='../css/images/modify.png' /><img title='取消推荐' tag='folder_unrecommand' src='../css/images/delete.png' /></div><img src='../css/images/folder.png' /><span>" + d.CategoryName + "</span></li>").appendTo(target);
                        li.data('cat', d).attr('catid', d.CategoryId);

                        i++;
                    }
                }
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        },
        complete: function() {
            $('#tab-recommandcategory').masklayer('hide');
        }
    });
}

function UnRecommandSltedCat() {
    var arr = [], id;
    $('#RecommandCategoryList').find(':checkbox[checked]').each(function() {
        id = $(this).parent().parent().attr('catid');
        if (id)
            arr.push(id);
    });
    if (arr == null || arr.length < 1)
        return;
    if (!confirm("您确认要取消选中的 " + arr.length + " 项的推荐状态吗？")) return;
    $('#content').masklayer('show');
    RecommandCategory.UnRecommandCategory(arr, function() {
        RefreshRecommandCategory();
        CheckSelectAllRecommandCatState();
        $('#content').masklayer('hide');
    }, function(err) {
        alert('操作失败，错误信息：' + err + '。');
        $('#content').masklayer('hide');
    });
}

function SelectAllRecommandCat() {
    var slted = $('#sltAllRecommandCat')[0].checked;
    $('#RecommandCategoryList').find(':checkbox').each(function() {
        this.checked = slted ? true : '';
    });
}

function CheckSelectAllRecommandCatState() {
    if ($('#RecommandCategoryList').find(':checkbox').length == 0) {
        $('#sltAllRecommandCat')[0].checked = '';
    } else if ($('#RecommandCategoryList').find(':checkbox').length == $('#RecommandCategoryList').find(':checkbox[checked]').length) {
        $('#sltAllRecommandCat')[0].checked = true;
    } else {
        $('#sltAllRecommandCat')[0].checked = '';
    }
}